package com.fast.controller.http;

import cn.hutool.http.HttpRequest;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.fast.controller.delay.Order;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.TimeUnit;

/*
  String hutool = HttpRequest.post("www.baidu.com")//路径 /Get
              .header("token", "token")//表头信息
              .body(JSONUtil.toJsonStr(param))//@requestBody的数据   **必须Json格式
              .form(param)//@requestParam表单中的数据    **必须放map
              .timeout(20000)//超时时间-包含ConnectTimeout和ReadTimeout
              .setConnectionTimeout(20000)//连接的时间  相当于ping通的时间
              .setReadTimeout(20000)//响应的时间        相当于发过去报文，等待处理的时间
              .execute().body();
 */
@RestController
@RequestMapping("/http")
@Api(tags = "http响应时间测试")
@Slf4j
public class HttpController extends ApiController {


    @GetMapping("/getSleep")
    @ApiOperation("线程睡眠6s")
    public String sleep() throws Exception {
//        log.info("user中的数据:{},睡眠时间为{}s", user, sleepTime);
//        Thread.sleep(sleepTime * 1000);
//        log.info("sleep处理完成");

        Order test1 = new Order("test1", 5, TimeUnit.SECONDS,"dsf");
        Order test2 = new Order("test2", 9, TimeUnit.SECONDS,"dfs");
        DelayQueue<Order> delayQueue = new DelayQueue<>();
        delayQueue.put(test1);
        delayQueue.put(test2);
        System.out.println(test1.name + "订单延迟队列开始时间:" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        System.out.println(test2.name + "订单延迟队列开始时间:" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        Thread thread = new Thread(() -> {
            while (delayQueue.size() != 0) {
                /**
                 * 取队列头部元素是否过期
                 */
                Order task = delayQueue.poll();
                if (task != null) {
                    System.out.format("订单:{%s}被取消, 取消时间:{%s}\n", task.name, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                }
            }
        });
        thread.start();

        return "sleep处理完成";
    }


    @GetMapping("/hutool")
    @ApiOperation("hutool的Http测试")
    public R hutool(Integer connectTimeout, Integer readTimeout, Integer sleepTime) {
        Map param = new HashMap();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("name", "zhangsan");
        hashMap.put("age", 12);
        param.put("user", hashMap);
        param.put("sleepTime", sleepTime);
        String body = HttpRequest.get("localhost:8084/http/getSleep").form(param)
                .setConnectionTimeout(connectTimeout * 1000).setReadTimeout(readTimeout * 1000).execute().body();
        return success(body);
    }


}
